﻿using Autofac;
using MediatR;
using MediatRTest.ConsulCore;
using MediatRTest.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SkyApm.Utilities.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TC.IOC.AutoFacEx;

namespace MediatRTest
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGrpc();
            services.AddSkyApmExtensions();
            //services.AddGrpc(option =>
            //{

            //    //可以由服务器接收的最大消息大小（以字节为单位）
            //    option.MaxReceiveMessageSize = 4 * 1024 * 1024; // 4 MB
            //    //可以从服务器发送的最大消息大小(以字节为单位)。设置为 null时，消息的大小不受限制
            //    option.MaxSendMessageSize = 1 * 1024 * 1024; // 1 MB
            //    //如果为 true，则当服务方法中引发异常时，会将详细异常消息返回到客户端
            //    option.EnableDetailedErrors = false;


            //    //用于压缩和解压缩消息的压缩提供程序的集合。默认已配置提供程序支持 gzip 压缩


            //    //压缩算法用于压缩从服务器发送的消息。 
            //    //该算法必须与 CompressionProviders 中的压缩提供程序匹配。 
            //    //若要使算法可压缩响应，客户端必须通过在 grpc-accept-encoding 标头中进行发送来指示它支持算法。
            //    option.ResponseCompressionAlgorithm = "gzip";

            //    //用于压缩从服务器发送的消息的压缩级别
            //    option.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Fastest;
            //});
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            
            app.UseRouting();
            app.UseGrpcWeb();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGrpcService<GreeterService>();
                endpoints.MapGrpcService<UserServ>().EnableGrpcWeb();
                endpoints.MapGrpcService<HealthCheckService>().EnableGrpcWeb();
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
                });
            });
            app.RegisterConsul(hostApplicationLifetime, new ConsulOption
            {
                ServiceName = "MediatRTest-Net5",
                ServiceIP = "172.16.102.164",
                ServicePort = 5001,
                Address = "http://localhost:8500"
            });
        }

        public void ConfigureContainer(ContainerBuilder builder)
        {
            builder.Register<ServiceFactory>(context =>
            {
                var c = context.Resolve<IComponentContext>();
                return t => c.Resolve(t);
            });
            _container = new AutoFacServiceLocator(builder);
            _container.MapAsPerLifetimeScope<IMediator, Mediator>();
            _container.ScanAssemblyAsPerLifetimeScope<Startup>();
            
            _container.UseAsDefault(false);//注意false和true的区别

        }

        public static AutoFacServiceLocator _container;
    }
}
